package ict;

import ict.mobile.R;
import android.app.Activity;
import android.os.Bundle;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.*;
import android.view.*;
import android.widget.*;

public class inventortyActivity extends Activity {
        SQLiteDatabase db;
        String sql;
        Cursor cursor = null;
        String[] columns = { "mid", "name", "password", "age" };
        String dataStrHeader = String.format("%4s %-12s %-9s %3s\n", "Mid", "Name",
                        "Password", "Age");
        String dataStr;
        TextView byNameTV, byCodeTV, displayTV;
        
        public static final int update = Menu.FIRST;
        
        

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.inventorydisplay);

                findViewBy();
                selectAllFromProductOrderByProductName();
                byNameTV.setBackgroundColor(R.drawable.white);
                byCodeTV.setBackgroundColor(0);
                
                byNameTV.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v){
                        selectAllFromProductOrderByProductName();
                        byNameTV.setBackgroundColor(R.drawable.white);
                        byCodeTV.setBackgroundColor(0);
                                }  
                }
                );
                
                byCodeTV.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v){
                        selectAllFromProductOrderByProductCode();
                        byCodeTV.setBackgroundColor(R.drawable.white);
                        byNameTV.setBackgroundColor(0);
                                }  
                }
                );
        }

        // ////find view by ID
        public void findViewBy() {
                byNameTV = (TextView) findViewById(R.id.byNameTV);
                byCodeTV = (TextView) findViewById(R.id.byCodeTV);
                displayTV = (TextView) findViewById(R.id.displayTV);

        }

        

        public boolean onCreateOptionsMenu(Menu menu) 
        {
            menu.add(0, update, 0, "Update inventorty Quantity").setIcon(R.drawable.edit);
            return true;
        }

        public boolean onOptionsItemSelected(MenuItem item) 
        {
            super.onOptionsItemSelected(item);
            switch( item.getItemId() ) 
            {
                case update:{
                	Intent intent = new Intent();
                	intent.setClass(this, updateQuantity.class);
                	startActivity(intent);
                	break;
                }     		
            	 
               	 	
            }
            return true;
            }

        /////display Product Order By Quantity
        public void selectAllFromProductOrderByQuantity(){
                try {
                        db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
                                        null, SQLiteDatabase.OPEN_READONLY);

                        cursor = db.rawQuery("select * from Product order by Quantity", null);
                        //Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();
                        
                        //ProductCode, ProductName, Quantity
                        String dataStr = "";
                        while (cursor.moveToNext()) {
                                // or cursor.getInt(0);
                                String ProductCode = cursor.getString(0);
                                String ProductName = cursor.getString(1);
                                int Quantity = cursor.getInt(2);
                                dataStr += ProductCode +'\t'+ ProductName +'\t'+ Quantity + '\n';
                        }
                        
                        displayTV.setText(dataStr);
                        
                        db.close();

                } catch (SQLiteException e) {
                        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
                }

        }

        /////display Product Order By ProductCode
        public void selectAllFromProductOrderByProductCode(){
                try {
                        db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
                                        null, SQLiteDatabase.OPEN_READONLY);

                        cursor = db.rawQuery("select * from Product order by ProductCode", null);
                        //Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();
                        
                        //ProductCode, ProductName, Quantity
                        String dataStr = "";
                        while (cursor.moveToNext()) {
                                // or cursor.getInt(0);
                                String ProductCode = cursor.getString(0);
                                String ProductName = cursor.getString(1);
                                int Quantity = cursor.getInt(2);
                                dataStr += ProductCode +'\t'+ ProductName + '\t'+Quantity + '\n';
                        }
                        
                        displayTV.setText(dataStr);
                        
                        db.close();

                } catch (SQLiteException e) {
                        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
                }

        }

        
        /////display Product Order By ProductName
        public void selectAllFromProductOrderByProductName(){
                try {
                        db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
                                        null, SQLiteDatabase.OPEN_READONLY);

                        cursor = db.rawQuery("select * from Product order by ProductName", null);
                        //Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();
                        
                        //ProductCode, ProductName, Quantity
                        String dataStr = "";
                        while (cursor.moveToNext()) {
                                // or cursor.getInt(0);
                                String ProductCode = cursor.getString(0);
                                String ProductName = cursor.getString(1);
                                int Quantity = cursor.getInt(2);
                                dataStr +=   ProductName +'\t'+ ProductCode +'\t'+ Quantity + '\n';
                        }
                        
                        displayTV.setText(dataStr);
                        
                        db.close();

                } catch (SQLiteException e) {
                        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
                }

        }
        
        
        @Override
        protected void onRestart() {
            super.onRestart();
            selectAllFromProductOrderByProductCode();
            byNameTV.setBackgroundColor(0);
            byCodeTV.setBackgroundColor(R.drawable.white);
            Toast.makeText(this, "Inventorty Successfull", Toast.LENGTH_SHORT).show();
        }
}

